use serde::{Deserialize, Serialize};
use std::pin::Pin;
use std::task::{Context, Poll};
use futures::Stream;
use chrono::{DateTime, Utc};

// 前端请求模型
#[derive(Debug, Deserialize)]
pub struct ChatRequest {
    pub message: String,
}

// AI响应模型
#[derive(Debug, Serialize)]
pub struct ChatResponse {
    pub response: String,
    pub model_used: String,
}

#[derive(Debug, Deserialize)]
pub struct ChatHistoryRequest{
    pub username: String
}

// DeepSeek API请求模型
#[derive(Debug, Serialize)]
pub struct DeepSeekRequest {
    pub model: String,
    pub messages: Vec<Message>,
    pub temperature: f32,
    pub max_tokens: u32,
    pub stream: bool,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Message {
    pub role: String,
    pub content: String
}

// DeepSeek API响应模型
#[derive(Debug, Deserialize)]
pub struct DeepSeekResponse {
    pub choices: Vec<Choice>,
}

#[derive(Debug, Deserialize)]
pub struct Choice {
    pub message: Message,
}

// 流式响应结构
pub struct ChatStream {
    pub inner: tokio::sync::mpsc::Receiver<String>,
}

impl Stream for ChatStream {
    type Item = Result<String, std::io::Error>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match self.inner.poll_recv(cx) {
            Poll::Ready(Some(data)) => Poll::Ready(Some(Ok(data))),
            Poll::Ready(None) => Poll::Ready(None),
            Poll::Pending => Poll::Pending,
        }
    }
}





// 聊天历史相关模型
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Conversation {
    pub id: i32,
    pub user_id: i32,
    pub datetime: DateTime<Utc>,
    pub first_message: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct HistoryRequest {
    pub username: String
}

#[derive(Debug, Serialize)]
pub struct HistoryResponse {
    pub conversations: Vec<Conversation>,
    pub total: i64,
}

#[derive(Debug, Deserialize)]
pub struct DeleteHistoryRequest{
    pub username: String
}
#[derive(Debug, Serialize)]
pub struct DeleteHistoryResponse{
    pub message: String
}


// 消息模型
#[derive(Debug, Serialize, Deserialize)]
pub struct MessageRecord {
    pub role: String,
    pub content: String,
    pub datetime: DateTime<Utc>
}

#[derive(Debug, Deserialize)]
pub struct MessageHistoryRequest{
    pub conversation_id: i32
}

#[derive(Debug, Serialize)]
pub struct MessageHistoryResponse{
    pub messages: Vec<MessageRecord>
}

#[derive(Debug, Deserialize)]
pub struct CreateConversationRequest{
    pub username: String
}

#[derive(Debug, Serialize)]
pub struct CreateConversationResponse{
    pub message: String,
    pub conversation_id: i32
}

#[derive(Debug, Deserialize)]
pub struct CreateMessageRequest{
    pub conversation_id: i32,
    pub role: String,
    pub content: String
}

#[derive(Debug, Serialize)]
pub struct CreateMessageResponse{
    pub message: String
}